package com.infore.airsupervise.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.infore.airsupervise.R;
import com.infore.airsupervise.utils.ImageUtil;

import java.util.List;

import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * 照片选择的适配器
 *
 * @author: dingxinlin
 * @date: 2016-10-19 16:33
 */
public class SelectPhotoAdapter extends RecyclerView.Adapter<SelectPhotoAdapter.ViewHolder> {

    private int itemWidth;
    private List<PhotoInfo> datas;

    private OnItemClickListener onItemClickListener = null;

    public SelectPhotoAdapter(List<PhotoInfo> datas, int itemWidth) {
        this.itemWidth = itemWidth;
        this.datas = datas;
    }

    /**
     * item点击监听接口
     */
    public interface OnItemClickListener {
        void onDeleteItemClick(View view, int position);
        void onAddItemClick(View view);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_photo, parent, false);
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.width = itemWidth;
        lp.height = itemWidth;
        view.setLayoutParams(lp);

        ViewHolder vh = new ViewHolder(view);

        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (datas.size() > 0) {
            if (position != datas.size()) {
                holder.delete.setVisibility(View.VISIBLE);
                PhotoInfo info = datas.get(position);
                String url = "file://"+info.getPhotoPath();
                ImageUtil.getInstance().displayImage(holder.imageView, url, itemWidth, itemWidth);
            } else {
                // 设置最后一个item的图片
                holder.imageView.setImageResource(R.drawable.ic_add_image);
                holder.delete.setVisibility(View.GONE);
            }
        } else {
            // 设置最后一个item的图片
            holder.imageView.setImageResource(R.drawable.ic_add_image);
            holder.delete.setVisibility(View.GONE);
        }

        // 添加按钮监听
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position == datas.size()) {
                    if (onItemClickListener != null)
                        onItemClickListener.onAddItemClick(view);
                }
            }
        });

        // 删除按钮监听
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null)
                    onItemClickListener.onDeleteItemClick(view, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size() + 1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ImageView delete;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.image_view);
            delete = (ImageView) itemView.findViewById(R.id.delete_btn);
        }
    }
}
